package stdlib.net

import java.net.Inet4Address
import java.net.InetAddress
import java.net.NetworkInterface
import java.util.*

private fun getIpAddress(): List<String> {
    try {
        val list: MutableList<String> = LinkedList()
        val enumeration = NetworkInterface.getNetworkInterfaces()
        while (enumeration.hasMoreElements()) {
            val network = enumeration.nextElement() as NetworkInterface
            if (network.isVirtual || !network.isUp) {
                continue
            } else {
                val addresses: Enumeration<*> = network.inetAddresses
                while (addresses.hasMoreElements()) {
                    val address = addresses.nextElement() as InetAddress
                    if (address is Inet4Address && !address.isLoopbackAddress) {
                        address.hostAddress?.let { list.add(it) }
                    }
                }
            }
        }
        return list
    } catch (e: Exception) {
        return listOf()
    }
}

fun main() {
    println(getIpAddress())
    println(getIpAddress().reduce { acc, s -> "$acc,$s" })
}